/*
 * music.js
 *
 * COS452
 * Alex Grant
 */
/*
 * musicObject
 * audioLib calls this to get the music
 */
function musicObject(tempo, notesPerBeat, sourceData) {
	this.tempo = tempo;
        this.notesPerBeat = notesPerBeat;
	this.sourceData = sourceData;
}
musicObject.prototype = {
                
	"tempo" : 120,
	"notesPerBeat" : 4,
		
	// for extended piano C0-D#8, 100 notes is 12-111
	"firstNoteOrdinal" : 49,		// 49 = C#3, which is ~138hz which is the first hearable frequency on my system
	"noteRange" : 60,                    // range starting at firstNoteOrdinal 60 = 5 octaves
	
	"note" : null,
                
	/* 
	 * noiseToFrequency
	 * Converts the given noise to an ordinal on the midi scale (in noteRange), 
	 * then to a musical note frequency in that range.
	 */
	"noiseToFrequency" : function (noise) {
		return 6.875 * Math.pow(2, (3 + ((Math.abs(noise) % this.noteRange) + this.firstNoteOrdinal)) / 12);
	},

	/*
	 * noiseToDuration
	 * Retrieves the next musical duration for the given noise input
	 */
	"noiseToDuration" : function (noise) {
		return 1/4;
	},

	//--------------------------------------------------------------------------------------------
	// public methods
	//--------------------------------------------------------------------------------------------

	/*
	 * getNextNote
	 * Gets the next note to play after the previous has expired.
	 * Returns a note object with "freq" and "dur" properties.
	 */
	"getNextNote" : function () {
		var data = this.sourceData.getSourceData();
		
		var frequency = this.noiseToFrequency(data.noise);
		var duration = this.noiseToDuration(data.noise);
		
		this.note = { freq : frequency, dur :  duration };
		//this.output(data.value, data.noise, frequency, duration);
		
		return this.note;
	},
	
	"getLastNote" : function () {
		return this.note;
	}, 
        
	"getNotesPerBeat" : function () {
		return this.notesPerBeat;
	},
        
	"getTempo" : function () {
		return this.tempo;
	}
}